﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Windows.Forms;
using IFractal = ArgusLib.Fractals.IFractal;
using IRenderer = ArgusLib.Fractals.IRenderer;

namespace Fraktalysator
{
    partial class MainForm
    {
		void fractal_CalculationFinished(IFractal sender, ArgusLib.Fractals.CalculationFinishedEventArgs e)
		{
			this.parameterBackUp = null;
			this.Invoke(new Action(() =>
				{
					this.formRendererParameter_RedrawRequested(null, EventArgs.Empty);
					this.formFractalParameter.SelectFractal(sender);
					this.formFractalParameter.InputControl.Parameter = sender.Parameter;
					this.tslStatusMessage.Text = Localization.Text.Message_CalculationFinished + ' ' + e.ElapsedTime.ToString(Localization.Formats.Time);
					this.formProgress.Close();
				}));
		}

		void fractal_CalculationAborted(IFractal sender, ArgusLib.Fractals.CalculationAbortedEventArgs e)
		{
			if (this.history.Count > 1)
				this.history.Pop();
			this.Invoke(new Action(() =>
				{
					this.tslStatusMessage.Text = Localization.Text.Message_CalculationAborted;
					if (this.parameterBackUp != null)
						this.formImage.ImageSize = new Size(this.parameterBackUp.PixelWidth, this.parameterBackUp.PixelHeight);
					this.parameterBackUp = null;
					this.formProgress.Close();
				}));
		}

		void fractal_CalculationProgressChanged(IFractal sender, ArgusLib.Fractals.CalculationProgressChangedEventArgs e)
		{
			this.formProgress.SetFractalProgress(e);
		}

		void formFractalParameter_CalculationRequested(object sender, EventArgs e)
		{
			IFractal newFractal = this.formFractalParameter.GetFractal();
			if (this.Fractal == null || newFractal.GetType() != this.Fractal.GetType())
			{
				this.formRendererParameter.InitializeRendererList(newFractal);
			}
			if (this.Fractal != null && newFractal.SupportsDataSetProvider(this.Fractal.DataSetProvider) == true)
			{
				newFractal.DataSetProvider = this.Fractal.DataSetProvider;
			}

			this.Fractal = newFractal;
			this.StartCalculation(true);
		}

		void formProgress_CancelRequested(object sender, EventArgs e)
		{
			this.Fractal.AbortCalculation(false);
		}

		void formRendererParameter_RedrawRequested(object sender, EventArgs e)
		{
			if (this.Fractal == null)
				return;
			IRenderer renderer = this.formRendererParameter.GetRenderer();

			if (renderer.SupportsDataSet(this.Fractal.DataSet) == false)
			{
				this.Fractal.DataSetProvider = this.formRendererParameter.DataSetProvider;
				if (MessageBox.Show(
					Localization.Text.MessageBox_RendererRecalculationRequest,
					Localization.Text.MessageBox_RendererRecalculationRequestCaption,
					MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.Yes)
				{
					this.StartCalculation(false);
				}
			}
			else
			{
				this.formImage.ImageSize = new System.Drawing.Size(this.Fractal.DataSet.DimensionX, this.Fractal.DataSet.DimensionY);
				Image image = this.formImage.Image;
				this.formImage.Image = renderer.Render(this.Fractal.DataSet);
				if (image != null)
					image.Dispose();
			}
		}

		void fractal_WarningReceived(IFractal sender, ArgusLib.Fractals.WarningReceivedEventArgs e)
		{
			string key;
			if (Enum.IsDefined(typeof(ArgusLib.Fractals.WarningReceivedEventArgs.Warnings), e.Warning) == true)
			{
				key = "Warning_" + Enum.GetName(typeof(ArgusLib.Fractals.WarningReceivedEventArgs.Warnings), e.Warning);
			}
			else
			{
				key = "Warning_" + e.Warning.ToString(System.Globalization.NumberFormatInfo.InvariantInfo);
			}

			string value = Localization.Text.ResourceManager.GetString(key);
			this.Invoke(new Action(() =>
				{
					if (MessageBox.Show(value, Localization.Text.MessageBox_WarningCaption, MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.No)
					{
						e.Abort = true;
					}
				}));
		}
    }
}
